package com.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cloud.Service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//http://localhost:8080/index
@RestController
public class ProviderController {
    //SPEL
    @Value("${server.port}")
    private String port;

    //http://localhost:8080/index
    //通过网关映射访问http://localhost:8010/provider/index?name=shu
    @GetMapping("/index")
    public String index() {
        return this.port;
    }

    //关联限流
    //http://localhost:8080/list
    @GetMapping("/list")
    public String list() {
        return "list";
    }

    @Autowired
    private ProviderService providerService;

    //http://localhost:8084/serviceTest1
    @GetMapping("/serviceTest1")
    public String serviceTest1() {
        this.providerService.test();
        return "serviceTest1";
    }

    @GetMapping("/serviceTest2")
    public String serviceTest2() {
        this.providerService.test();
        return "serviceTest2";
    }

    //热点限流
    //http://localhost:8084/hot?num1=23&num2=45
    @GetMapping("/hot")
    @SentinelResource("hot")
    public String hot(
            @RequestParam(value = "num1",required = false) Integer num1,
            @RequestParam(value = "num2",required = false) Integer num2){
        return num1+"-"+num2;
    }

    //基于PAI分组的限流
    //http://localhost:8084/api1/demo1?name=shu
    @GetMapping("/api1/demo1")
    public String demo1(){
        return "demo";
    }

    @GetMapping("/api1/demo2")
    public String demo2(){
        return "demo";
    }

    @GetMapping("/api2/demo1")
    public String demo3(){
        return "demo";
    }

    @GetMapping("/api2/demo2")
    public String demo4(){
        return "demo";
    }
}
